<?php
class ModelPromotionPromotion extends Model {
	public function updateViewed($activity_id) {
		$this->db->query("UPDATE " . DB_PREFIX . "promotion_activity SET click_num = (click_num + 1) WHERE id = '" . (int)$activity_id . "'");
	}

	public function getActivitys($limit) {				
		$activity_data = $this->cache->get('promotion.activity.all.' . (int)$this->config->get('config_language_id') . '.' . (int)$limit);

		if (!$activity_data) {
			$query = $this->db->query("SELECT a.* FROM " . DB_PREFIX . "promotion_activity a WHERE a.status = 1 AND a.start_time <= NOW() " . 
			"AND a.end_time >= NOW() AND lang_id = '" . (int)$this->config->get('config_language_id') . 
			"' ORDER BY a.start_time DESC LIMIT " . (int)$limit);
			$activity_data = $query->rows;
			$this->cache->set('promotion.activity.' . (int)$this->config->get('config_language_id') . '.' . (int)$limit, $activity_data);
		}
		
		return $activity_data;
	}
	
	public function getActivityById($id) {
		
		$activity_data = $this->cache->get('promotion.activity.id' . (int)$this->config->get('config_language_id') . '.' . (int)$id);

		if (!$activity_data) {
			$query = $this->db->query("SELECT a.* FROM " . DB_PREFIX . 
				"promotion_activity a WHERE a.id ='" . $id . "' and a.status = 1 AND a.start_time <= NOW() AND a.end_time >= NOW()");
			$activity_data = $query->row;
			$this->cache->set('promotion.activity.id' . (int)$this->config->get('config_language_id') . '.' . (int)$id, $activity_data);
		}
		return $activity_data;
	}
}
?>